<?php
/*
    BlogEffects 0.1b2
    http://blogeffects.net/
    Copyright (C) 2007 Chad Ohman (chad@chadohman.com) All rights reserved.

    BlogEffects is distributed WITHOUT WARRANTY WHATSOEVER.

    Last developer edit: January 2, 2008 9:50a
*/

// Connect to MySQL
function configure() {
	global $mysql;
	mysql_connect($mysql['server'], $mysql['user'], $mysql['pass']) or error("1");
	mysql_select_db($mysql['database']) or error("2");
}

// Include post functions
require 'template/posts.php';

function blog_rss($numposts) {
	global $config;
	
	$rss = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
	$rss .= '<rss version="2.0">'."\n";
	$rss .= '<channel>'."\n";
	$rss .= '<title>'.$config['sitename'].'</title>'."\n";
	$rss .= '<link>http://'.$_SERVER['HTTP_HOST'].'/</link>'."\n";
	$rss .= '<description>RSS Feed for '.$config['sitename'].'</description>'."\n";
	$rss .= '<language>en-us</language>'."\n";
	$rss .= '<generator>BlogEffects (version 0.1a3)</generator>'."\n";
	
	$result = mysql_query("SELECT * FROM `posts` ORDER BY `timestamp` DESC LIMIT ".$numposts);
	
	while ($post = mysql_fetch_array($result)) {
		$rss .= '<item>'."\n";
		$rss .= '<title>'.$post['title'].'</title>'."\n";
		$rss .= '<link>http://'.$_SERVER['HTTP_HOST'].'/?post='.$post['id'].'</link>'."\n";
		$rss .= '<guid>http://'.$_SERVER['HTTP_HOST'].'/?post='.$post['id'].'</guid>'."\n";
		$rss .= '<content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA['.$post['text'].']]></content:encoded>'."\n";
		$rss .= '<pubDate>'.date("r", $post['timestamp']).'</pubDate>'."\n";
		$rss .= '</item>'."\n";
	}
	
	$rss .= '</channel>'."\n";
	$rss .= '</rss>';
	echo $rss;
}

// Comments
function get_comments($postid) {
	$query = mysql_query("SELECT * FROM `comments` WHERE `pid` = '".$postid."' ORDER BY `time` DESC") or die(mysql_error());
	$count = 0;
	while (list($id[$count], $pid[$count], $time[$count], $name[$count], $email[$count], $website[$count], $text[$count]) = mysql_fetch_array($query)) {
		$time[$count] = date("F j, Y", $time[$count]);
		$comments[$count]['id']      = $id[$count];
		$comments[$count]['pid']     = $pid[$count];
		$comments[$count]['time']    = $time[$count];
		$comments[$count]['name']    = $name[$count];
		$comments[$count]['email']   = $email[$count];
		$comments[$count]['website'] = $website[$count];
		$comments[$count]['text']    = $text[$count];
		$count++;
	}
	return $comments;
}

// Allow for errors to be relayed to the user
function error($error_number) {
	if ($error_number == "1") {
		echo "I could not connect to the MySQL server. Here's a (semi-)useful error: ".mysql_error();
		die;
	} elseif ($error_number == "2") {
		echo "I could not select database '".$mysql['database']."' due to this (semi-)useful error: ".mysql_error();
		die;
	} elseif ($error_number == "3") {
		echo "I could not fetch the data from the MySQL server. Instead, have a (semi-)useful error: ".mysql_error();
		die;
	}
}
?>